package com.xszx.controller;

import com.xszx.entity.*;
import com.xszx.entity.dto.RecordDTO;
import com.xszx.entity.enums.AssetEnmu;
import com.xszx.entity.enums.ConStatusEnum;
import com.xszx.entity.enums.ConTypeEnum;
import com.xszx.service.TAssetService;
import com.xszx.service.TContractService;
import com.xszx.service.TDeptService;
import com.xszx.service.TRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
public class RecordController {

    @Autowired
    private TRecordService recordService;

    @Autowired
    private TContractService contractService;

    @Autowired
    private TDeptService deptService;

    @Autowired
    TAssetService assetService;


    @RequestMapping("/selectRecordDTO")
    @ResponseBody
    public List<RecordDTO> selectTRecord(HttpServletRequest request){

        TUser user = (TUser) request.getSession().getAttribute("user");

        List<RecordDTO> recordDTOS = recordService.selectRecording(user.getDid());
        log.info("遍历申请表：{}",recordDTOS);
//        for (RecordDTO recordDTO : recordDTOS) {
//            Date borrowdate = recordDTO.getBorrowdate();
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            String format1 = format.format(borrowdate);
//        }
        System.out.println("查询出的记录结果是："+recordDTOS);
        return recordDTOS;
    }

    //@RequestBody TDept dept
    @PostMapping("/updateRecord")
    @ResponseBody
    public int updateRecord(TRecord record){
        log.info("修改申请：{}",record);
            int i = recordService.updateRecord(record);
            System.out.println( i> 0 ? "修改成功！" : "修改失败！");
            System.out.println("受影响的行数为：" + i);
        return i;
    }

    @RequestMapping("/getRecordDTOByRid")
    @ResponseBody
    public RecordDTO getRecordDTOByRid(@RequestParam("rid")Long rid, HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        log.info("根据id修改申请：{}",rid);
       RecordDTO recordDTO =  recordService.getRecordDTOByRid(rid);
        Date borrowdate = recordDTO.getBorrowdate();
        //根据aid查找aname
        TAsset asset = assetService.selectAssetByAid(recordDTO.getAid(), user.getDid());
        log.info("获取到的资产：{}",asset);
        recordDTO.setAname(asset.getAname());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(borrowdate);
        System.out.println(format1);
        recordDTO.setBorrowdate1(format1);
        System.out.println(recordDTO);
        recordDTO.setUsername(user.getUsername());
        return  recordDTO;
    }


    @RequestMapping("/getRecordCount")
    @ResponseBody
    public int getRecordCount(){
        return recordService.selectRecordings().size();
    }

    /**
     * 获取审批结果
     * @param request
     * @param rid  记录ID
     * @param id  审批结果  1：通过  2：驳回
     */
    @RequestMapping("/obtainApprovalResult")
    @ResponseBody
    public int obtainApprovalResult(HttpServletRequest request,@RequestParam("rid")Long rid,int id) throws ParseException {
        System.out.println("进入审批方法。。。");
        //获取登录用户信息
        TUser user = (TUser) request.getSession().getAttribute("user");
        TDept dept = deptService.getDeptByDid(user.getDid());

        RecordDTO recordDTO = recordService.getRecordDTOByRid(rid);
        log.info("获取到的审批数据是："+recordDTO);

        //获取审批结果
        String name = ConStatusEnum.getName(id);
        recordDTO.setRstatus(name);
       recordDTO.setRedate(new Date());

           //修改审批表的状态
           int i = recordService.updateStatus(recordDTO);
           log.info("对应记录的审批结果是："+(i>0?"成功":"失败"));
           //
           TContract tContract = contractService.selectContractByAid(recordDTO.getAid(),ConStatusEnum.theApprovalIsPending.getName());
           log.info("根据aid查询出的数据是："+tContract);
           int udpateContractStatus = contractService.udpateContractStatus(tContract.getCid(), name);
           log.info("修改合同状态的操作执行结果是："+(udpateContractStatus>0?"成功":"失败"));

           /*
            * 修改资产表中的部门名称为合同表中的乙方部门
            * 参数1：记录表中的资产ID
            * 参数2：合同表中的乙方部门名称
            * 参数3：根据用户session中获取出的部门ID
            * */
        String rtype = recordDTO.getRtype();
        if (rtype.equals("资产调度")) {
            int updateAintodeptByAid = assetService.updateAstatusByAid(recordDTO.getAid(), AssetEnmu.inservice.getName());
            log.info("修改资产调度后的部门名称的操作执行结果是：" + (updateAintodeptByAid > 0 ? "成功" : "失败"));
            return updateAintodeptByAid;
        }else if (rtype.equals("资产维护")){
            int updateAintodeptByAid = assetService.updateAstatusByAid(recordDTO.getAid(), AssetEnmu.maintains.getName());
            log.info("修改资产维护后的部门名称的操作执行结果是：" + (updateAintodeptByAid > 0 ? "成功" : "失败"));
            return updateAintodeptByAid;
        }else if (rtype.equals("资产报废")){
            int updateAintodeptByAid = assetService.updateAstatusByAid(recordDTO.getAid(), AssetEnmu.scrap.getName());
            log.info("修改资产报废后的部门名称的操作执行结果是：" + (updateAintodeptByAid > 0 ? "成功" : "失败"));
            return updateAintodeptByAid;
        }
        return -1;
    }


}
